﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;

#endregion

namespace WinnerCMS.WebUI.Admin.Role
{
    [Access]
    public partial class Add : Page
    {
        private readonly RoleManage _myBll = new RoleManage();
        protected string strOperation = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var action = Request["Action"];

                if (action == "ReadOnly")
                {
                    var model = this._myBll.GetInfo(int.Parse(Request["ID"]));

                    this.txtRoleName.Text = model.RoleName;
                    var rightIds = (from x in new RightsManage().GetRoleRights(model.Id) select x.Id).ToArray();
                    this.strOperation = string.Join(",", rightIds);

                    GetPower();
                    this.btnAdd.Visible = false;
                    this.cbkALL.Visible = false;
                }
                else if (Request["ID"] == null)
                {
                    this.btnAdd.Text = "添加";
                    GetPower();
                }
                else
                {
                    var model = this._myBll.GetInfo(int.Parse(Request["ID"]));

                    this.txtRoleName.Text = model.RoleName;
                    var rightIds = (from x in new RightsManage().GetRoleRights(model.Id) select x.Id).ToArray();
                    this.strOperation = string.Join(",", rightIds);

                    GetPower();
                    this.btnAdd.Text = "修改";
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Result res;
            if (Request["ID"] == null)
            {
                var model = new WinnerCMS.Model.Role
                {
                    RoleName = this.txtRoleName.Text.Trim(),
                    Remark = "",
                    IsAdmin = true,
                    Rank = 0
                };

                res = new RoleManage().Add(model, Request.Form["chkOperation"] ?? string.Empty);
            }
            else
            {
                var model = this._myBll.GetInfo(Convert.ToInt32(Request["ID"]));
                model.RoleName = this.txtRoleName.Text.Trim();

                res = this._myBll.Edit(model, Request.Form["chkOperation"]);
            }

            if (res.Errors.Length > 0)
            {
                MsgBox.Message(res.Errors, this);
            }
            else
            {
                Response.Redirect("Manage.aspx");
            }
        }


        private void GetPower()
        {
            this.DataList1.DataSource = new RightsManage().GetRights(true);
            this.DataList1.DataBind();
        }

        protected IList<Right> BindPower(object rootId)
        {
            return new RightsManage().GetRights((int) rootId, true);
        }
    }
}